package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper1;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper1 setmealMapper1;
    //分页查询
    @Override
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO) {
        //非空查询
        if(categoryPageQueryDTO.getPage()==null){
        categoryPageQueryDTO.setPage(1);
        }
        if (categoryPageQueryDTO.getPageSize()==null){
            categoryPageQueryDTO.setPageSize(5);
        }
        //进行分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        //调用mapper方法
        Page<Category> byIdPage = categoryMapper.findByIdPage(categoryPageQueryDTO);
        //封装
        PageResult pageResult = new PageResult();
        pageResult.setTotal(byIdPage.getTotal());
        pageResult.setRecords(byIdPage.getResult());
        //返回值

        return Result.success(pageResult);
    }
    //分类查询
    @Override
    public Result list(Integer type) {
        //非空判断
//        if(type==null){
//            return Result.error(MessageConstant.UNKNOWN_ERROR);
//        }
        //调用mapper方法
        List<Category> list = categoryMapper.list(type);
        //返回
        return Result.success(list);
    }

    //    新增分类
    @Override
    @Transactional//开启事务
    public Result add(CategoryDTO categoryDTO) {
        //非空判断
        if(categoryDTO.getName()==null){
        return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if(categoryDTO.getSort()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //进行拷贝
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //代码补充
        category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper
        categoryMapper.insert(category);
        //返回值
        return Result.success();
    }
    //修改启用类型
    @Override
    @Transactional//开启事务
    public Result updatestatus(Integer status, Long id) {
        //进行非空判断
        if(status==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if (id==null){
            return Result.error(MessageConstant.ACCOUNT_NOT_CM);
        }
        //调用mapper接口进行修改
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        category.setUpdateUser(BaseContext.getCurrentId());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
        //返回值
        return Result.success();
    }
    //修改
    @Override
    @Transactional//开启事务
    public Result update(CategoryDTO categoryDTO) {
        //非空判断
        if(categoryDTO.getName()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        if(categoryDTO.getSort()==null){
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        //拷贝
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //补充
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        //调用mapper
        categoryMapper.update(category);
        //返回
        return Result.success();
    }
    //删除
    @Override
    @Transactional//开启事务
    public Result date(Long id) {
        //非空判断
        if(id==null){
            return Result.error(MessageConstant.ACCOUNT_NOT_CM);
        }
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer categoryId = dishMapper.countByCategoryId(id);
        if (categoryId>0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        categoryId = setmealMapper1.countByCategoryId(id);
        if(categoryId > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteId(id);
        //返回值
        return Result.success();
    }
}
